package cn.tedu.yl.user.service.impl;

import cn.tedu.yl.base.exception.ServiceException;
import cn.tedu.yl.base.response.PageData;
import cn.tedu.yl.base.response.StatusCode;
import cn.tedu.yl.base.response.TokenService;
import cn.tedu.yl.user.mapper.UserMapper;
import cn.tedu.yl.user.pojo.dto.UserDelParam;
import cn.tedu.yl.user.pojo.dto.UserLoginParam;
import cn.tedu.yl.user.pojo.dto.UserQuery;
import cn.tedu.yl.user.pojo.dto.UserSaveParam;
import cn.tedu.yl.user.pojo.entity.User;
import cn.tedu.yl.user.pojo.vo.UserVO;
import cn.tedu.yl.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    TokenService tokenService;

    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("登录业务,参数:{}",userLoginParam);
        //根据前端传来的用户名查询数据库中是否有此用户的数据 Alt+Enter
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        /* 如果业务层遇到了问题,不可以直接给前端响应,要抛出异常,由全局异常处理器来处理! */
        if(userVO == null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if(!userVO.getPassword().equals(userLoginParam.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }

        //生成带有时效的token
        String token = tokenService.createToken("user");
        userVO.setToken(token);

        /* 如果没有抛出异常,业务层会将正常处理完毕的userVO结果返回给Controller层
        再由Controller层响应给前端 */
        return userVO;
    }

    @Override
    public void saveUser(UserSaveParam userSaveParam) {
        log.info("保存用户业务,参数:{}",userSaveParam);
        User user = new User();
        BeanUtils.copyProperties(userSaveParam,user);
        //需要判断是新增还是修改操作
        if(user.getId()==null){
            user.setCreateTime(new Date());//设置创建时间为当前时间
            userMapper.insert(user);//调用数据层新增用户
        }else {
            System.out.println(user);
            user.setCreateTime(new Date());//设置创建时间为当前时间
            userMapper.update(user);
        }
    }


    //获取管理员管理列表数据
    @Override
    public PageData getList(UserQuery userQuery) {
        log.info("获取管理员管理界面参数：{}",userQuery);
        List<UserVO> list = userMapper.selectList(userQuery);//调用数据层新增用户

        if(list.size() <= 0){
            throw new ServiceException(StatusCode.DATA_UNEXISTS);
        }
        int total =userMapper.selListCount(userQuery);
        PageData pageData = new PageData();
        pageData.setList(list);
        pageData.setTotal(total);
        return pageData;

    }

    //获取用户详情
    @Override
    public UserVO detail(Long id) {
        log.info("获取用户详情id:{}",id);
        UserVO userVO = userMapper.selectById(id);
        return userVO;
    }


    //批量删除业务
    @Override
    public void delete(UserDelParam userDelParam) {
        log.info("获取删除业务参数数组,{}",userDelParam);

        if(userDelParam !=null){
            userMapper.delete(userDelParam.getArrIds());
        }

    }

}

























